package org.asiainfo.audit.service.impl;

import org.asiainfo.audit.domain.vo.TbAuditMessageVo;
import org.asiainfo.audit.domain.bo.TbAuditMessageBo;
import org.asiainfo.audit.domain.TbAuditMessage;
import org.asiainfo.audit.service.TbAuditMessageService;
import org.asiainfo.common.core.utils.StringUtils;
import org.asiainfo.common.mybatis.core.page.PageQuery;
import org.asiainfo.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import org.asiainfo.common.core.utils.MapstructUtils;
import lombok.extern.slf4j.Slf4j;
import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.asiainfo.audit.mapper.TbAuditMessageMapper;
import com.mybatisflex.core.paginate.Page;

import java.util.List;
import java.util.Map;

/**
 * 审计信息(TbAuditMessage)表服务实现类
 *
 * @author dotor-ww
 * @since 2024-01-16 23:46:12
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class TbAuditMessageServiceImpl extends ServiceImpl<TbAuditMessageMapper, TbAuditMessage> implements TbAuditMessageService {

    /**
     * 构建查询条件
     *
     * @param tbAuditMessageBo 审计信息
     * @return 构建条件
     */
    private QueryWrapper builderQueryWrapper(TbAuditMessageBo tbAuditMessageBo) {
        Map<String, Object> params = tbAuditMessageBo.getParams();
        QueryWrapper query = query();
        query.like(TbAuditMessage::getModule, tbAuditMessageBo.getModule(), StringUtils.isNotBlank(tbAuditMessageBo.getModule()));
        query.like(TbAuditMessage::getUrl, tbAuditMessageBo.getUrl(), StringUtils.isNotBlank(tbAuditMessageBo.getUrl()));
        query.like(TbAuditMessage::getUserName, tbAuditMessageBo.getUserName(), StringUtils.isNotBlank(tbAuditMessageBo.getUserName()));
        query.eq(TbAuditMessage::getHostIp, tbAuditMessageBo.getHostIp(), StringUtils.isNotBlank(tbAuditMessageBo.getHostIp()));
        query.between(TbAuditMessage::getCreateTime, params.get("beginTime"), params.get("endTime"),
            ObjectUtil.isNotNull(params.get("beginTime")) && ObjectUtil.isNotNull(params.get("endTime")));
        return query;
    }

    /**
     * 通过查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TbAuditMessageVo queryById(Long id) {
        return getOneAs(query().eq(TbAuditMessage::getId, id), TbAuditMessageVo.class);
    }

    /**
     * 分页查询
     *
     * @param tbAuditMessageBo 筛选条件
     * @param pageQuery        分页对象
     * @return 查询结果
     */
    @Override
    public TableDataInfo<TbAuditMessageVo> queryPageList(TbAuditMessageBo tbAuditMessageBo, PageQuery pageQuery) {
        QueryWrapper query = builderQueryWrapper(tbAuditMessageBo);
        pageQuery.buildOrder(query);
        if (StringUtils.isBlank(pageQuery.getOrderBy())) {
            query.orderBy(TbAuditMessage::getCreateTime).desc();
        }
        Page<TbAuditMessageVo> page = pageAs(pageQuery.buildPage(), query, TbAuditMessageVo.class);
        return TableDataInfo.build(page);
    }

    /**
     * 新增数据
     *
     * @param tbAuditMessageBos 实例对象
     */
    @Override
    public void insertBatchByBo(List<TbAuditMessageBo> tbAuditMessageBos) {
        List<TbAuditMessage> tbAuditMessages = MapstructUtils.convert(tbAuditMessageBos, TbAuditMessage.class);
        saveBatch(tbAuditMessages);
    }
}
